from py2neo import Node, Relationship
from neo4j import get_neo4j_connect
from neo4j.data import *
# TODO: 将data.py的数据换成resource里的文件的加载方式, 或者其他方式, 不应该写在代码里
from neo4j.labels import *
# TODO:labels应该也是数据, 不应写在代码里

from server.config import neo4j_url, neo4j_user_name, neo4j_password

graph = get_neo4j_connect(neo4j_url, neo4j_user_name, neo4j_password, True, 5)

# TODO: 简化一般化, 改成文件存储, 修改整个项目的find_one函数, 这个
#  函数已经在py2neo 4以上版本已经废弃, 换成新版本相应的操作:


def create():
    graph.create(Node(attribute, name='名称', name2='名称'))
    graph.create(Node(attribute, name='别名', name2='别名'))
    graph.create(Node(attribute, name='开放时间', name2='开放时间'))
    graph.create(Node(attribute, name='开放日', name2='开放日'))
    graph.create(Node(attribute, name='电话', name2='电话'))
    graph.create(Node(attribute, name='描述', name2='描述'))
    graph.create(Node(attribute, name='借阅时间', name2='借阅时间'))
    # graph.create(Node(attribute, name='借阅',name2='借阅'))
    graph.create(Node(attribute, name='位置', name2='位置'))
    graph.create(Node(attribute, name='坐标', name2='坐标'))
    graph.create(Node(attribute, name='时间', name2='时间'))
    graph.create(Node(attribute, name='数量', name2='数量'))
    graph.create(Node(attribute, name='年龄', name2='年龄'))
    graph.create(Node(attribute, name='功能', name2='功能'))
    graph.create(Node(attribute, name='类别', name2='类别'))
    graph.create(Node(attribute, name='作者', name2='作者'))
    graph.create(Node(attribute, name='主题', name2='主题'))
    graph.create(Node(attribute, name='收藏时间', name2='收藏时间'))
    # -----------------------------------------------------------------
    graph.create(Node(library, name='国家图书馆'))
    graph.create(Node(room, name='馆室'))
    graph.create(Node(area, name='馆区'))
    graph.create(Node(floor, name='楼层'))
    graph.create(Node(card, name='读者卡'))
    graph.create(Node(resource, name='资源'))
    graph.create(Node(restype, name='资源初级类型'))
    graph.create(Node(mul_type, name='资源中级类型'))
    graph.create(Node(res, name='资源大类'))
    graph.create(Node(service, name='服务'))
    graph.create(Node(task, name='业务'))
    graph.create(Node(goods, name='精品'))
    # -----------------------------------------------------------------
    graph.create(
        Relationship(
            graph.find_one(
                label=library
            ), '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='位置')
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=library),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        ))
    graph.create(
        Relationship(
            graph.find_one(label=library),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=library),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='电话'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=library),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='电话'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=library),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=area),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=area),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=area),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='电话'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=area),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='位置'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=floor),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='位置'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )
    graph.create(Relationship(
        graph.find_one(label=room),
        '属性',
        graph.find_one(
            label=attribute,
            property_key='name',
            property_value='坐标'
        )
    ))
    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='开放日'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='开放时间'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='电话'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=room),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='借阅时间'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='数量'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='主题'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='收藏时间'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=goods),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='类别'
            )
        )
    )
    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='作者'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=restype),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=restype),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=restype),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=restype),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='数量'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=mul_type),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=mul_type),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=mul_type),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=mul_type),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='数量'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=res),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=res),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=res),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    graph.create(
        Relationship(graph.find_one(label=res),
                     '属性',
                     graph.find_one(
                         label=attribute,
                         property_key='name',
                         property_value='数量'
                     )
                     )
    )

    graph.create(
        Relationship(
            graph.find_one(label=card),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=card), '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=card),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=card),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='年龄'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=card),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='功能'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=service),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='时间'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=service),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=service),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=service),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=task),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='时间'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=task),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='名称'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=task),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='别名'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=task),
            '属性',
            graph.find_one(
                label=attribute,
                property_key='name',
                property_value='描述'
            )
        )
    )

    # -----------------------------------------------------------------

    graph.create(
        Relationship(
            graph.find_one(label=room),
            '位于',
            graph.find_one(label=floor)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=area),
            '包括',
            graph.find_one(label=library)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=room),
            '处于',
            graph.find_one(label=area)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=floor),
            '处于',
            graph.find_one(label=area)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=goods),
            '属于',
            graph.find_one(label=resource)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '属于',
            graph.find_one(label=restype)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=restype),
            '属于',
            graph.find_one(label=mul_type)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=restype),
            '属于',
            graph.find_one(label=res)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=resource),
            '存放',
            graph.find_one(label=room)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=goods),
            '存放',
            graph.find_one(label=room)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=room),
            '证件',
            graph.find_one(label=card)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=service),
            '发生',
            graph.find_one(label=room)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=task),
            '发生',
            graph.find_one(label=room)
        )
    )

    graph.create(
        Relationship(
            graph.find_one(label=service),
            '对象',
            graph.find_one(label=resource)
        )
    )


def create_node():
    """
    建立馆室节点: 导入数据
    """
    room_values = room_data.split('@')
    for i in range(1, len(room_values) - 6):
        row = room_values[i].split(' ')
        name = row[0]
        graph.create(Node(
            room, type=room, name=name,
            office_name=row[0], variant_name=row[1],
            position=row[2], describe=row[3],
            open_date=row[4], phone=row[5],
            work_open=row[6], work_borrow=row[7],
            week_open=row[8], week_borrow=row[9],
            area=row[10], floor=row[11],
            card=row[12],
            des_x=row[15],
            des_y=row[16],
            borrow=row[19],
            open=row[20]
        ))
    for i in range(len(room_values) - 6, len(room_values)):
        row = room_values[i].split(' ')
        mark_node = Node(
            mark, type=mark, name=row[0],
            office_name=row[0], self_site=row[14])
        graph.create(mark_node)

    # 建立馆区节点
    area_values = area_data.split('@')
    for i in range(1, len(area_values)):
        row = area_values[i].split(' ')
        name = row[0]
        graph.create(Node(
            area, type=area, name=name,
            office_name=row[0],
            variant_name=row[1],
            position=row[2],
            date=row[3],
            worktime=row[5],
            weektime=row[6]
        ))

    # 建立楼层节点
    floor_values = floor_data.split('@')
    for i in range(1, len(floor_values)):
        row = floor_values[i].split(' ')
        name = row[0]
        graph.create(Node(
            floor, type=floor, name=name,
            office_name=row[0], variant_name=row[1],
            area=row[2],
            upstair=row[3], downstair=row[4]
        ))

    # 建立资源节点
    resource_values = resource_data.split('@')
    for i in range(1, len(resource_values)):
        row = resource_values[i].split(' ')
        name = row[0]
        graph.create(Node(
            resource, type=resource, name=name,
            office_name=row[0], variant_name=row[1],
            describe=row[2], count=row[3],
            room=row[4],
            belong=row[5],
            collection_time=row[6],
            form=row[8],
            topic=row[9]
        ))

    # 建立精品节点
    goods_values = goods_data.split('@')
    for i in range(1, len(goods_values)):
        row = goods_values[i].split(' ')
        name = row[0]
        graph.create(Node(
            goods, type=goods, name=name,
            office_name=row[0], variant_name=row[1],
            kind=row[2], writer=row[3]
        ))

    # 建立证件节点
    card_values = card_data.split('@')
    for i in range(1, len(card_values)):
        row = card_values[i].split(' ')
        card_node = Node(
            card, type=card, name=row[0],
            office_name=row[0], variant_name=row[1],
            age=row[2], function=row[3]
        )
        graph.create(card_node)

    # 建立服务节点
    service_values = service_data.split('@')
    for i in range(1, len(service_values)):
        row = service_values[i].split(' ')
        service_node = Node(
            service, type=service, name=row[0],
            office_name=row[0], variant_name=row[1],
            date=row[3],
            worktime=row[4], weektime=row[5],
            discribe=row[6], card=row[7]
        )
        graph.create(service_node)

    # 建立业务节点
    task_values = task_data.split('@')
    for i in range(1, len(task_values)):
        row = task_values[i].split(' ')
        print('row[3]', row[3], row[0])
        task_node = Node(
            task, type=task, name=row[0],
            office_name=row[0], variant_name=row[1],
            date=row[3],
            worktime=row[4], weektime=row[5], discribe=row[6],
            card=row[7]
        )
        graph.create(task_node)

    # 建立资源中级类型节点
    multi_type_values = mulType_data.split('@')
    for i in range(1, len(multi_type_values)):
        row = multi_type_values[i].split(' ')
        graph.create(Node(
            mul_type, type=mul_type, name=row[0],
            office_name=row[0], variant_name=row[1]))

    # 建立资源类型节点
    res_type_values = resType_data.split('@')
    for i in range(1, len(res_type_values)):
        # print(row[4],row[3])
        row = res_type_values[i].split(' ')
        restype_node = Node(restype, type=restype, name=row[0],
                            office_name=row[0], variant_name=row[1],
                            describe=row[2], kind=row[3],
                            count=row[4],
                            belong=row[5])
        graph.create(restype_node)

    # 建立国家图书馆节点
    describe = '国家图书馆的前身是建于1909年（清宣统元年）9月9日的京师图书馆，' \
               '1912年8月27日开馆接待读者，' \
               '1916年京师图书馆按规定正式接受国内出版物呈缴本，' \
               '开始履行国家图书馆的部分职能，1928年7月更名为国立北平图书馆，' \
               '1929年8月与北平北海图书馆合并，仍名国立北平图书馆， ' \
               '1950年3月6日国立北平图书馆更名为国立北京图书馆，' \
               '1951年6月12日更名为北京图书馆，' \
               '1998年12月12日经国务院批准，北京图书馆更名为国家图书馆，' \
               '对外称中国国家图书馆。包括文津街古籍馆、' \
               '白石桥总馆南区和总馆北区三个馆区。'
    graph.create(
        Node(
            library, type=library, name=library,
            office_name=library, variant_name='图书馆，国家图书馆，国图',
            describe=describe, phone='010-88545114'
        )
    )

    # 建立资源大类
    res_values = [['数字资源', 19609100], ['实体资源', 39011882]]
    variant_name = '数字资源，电子资源，数据库资源，数据库'
    graph.create(Node(
        res, type=res, name=res_values[0][0],
        office_name=res_values[0][0], variant_name=variant_name,
        num=res_values[0][1]
    ))
    graph.create(Node(
        res, type=res, name=res_values[1][0],
        office_name=res_values[1][0],
        variant_name=res_values[1][0], num=res_values[1][1]
    ))


def create_relation():
    """
    建立馆室联系
    """
    room_values = room_data.split('@')
    for i in range(1, len(room_values) - 6):
        try:
            row = room_values[i].split(' ')
            if row[10].find(u'，') != -1:
                area_arr = row[10].split(u'，')
                for sub_area in area_arr:
                    graph.create(
                        Relationship(
                            graph.find_one(
                                label=room,
                                property_key='office_name',
                                property_value=row[0]
                            ), '位于',
                            graph.find_one(
                                label=area,
                                property_key='office_name',
                                property_value=sub_area
                            )
                        )
                    )
            else:
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=row[0]), '处于',
                        graph.find_one(
                            label=area,
                            property_key='office_name',
                            property_value=row[
                                10]
                        )
                    )
                )
            if row[11].find(u'，') != -1:
                floor_arr = row[11].split(u'，')
                for sub_floor in floor_arr:
                    graph.create(
                        Relationship(
                            graph.find_one(
                                label=room,
                                property_key='office_name',
                                property_value=row[0]
                            ), '位于',
                            graph.find_one(
                                label=floor,
                                property_key='office_name',
                                property_value=sub_floor
                            )
                        )
                    )
            else:
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=row[0]
                        ),
                        '位于',
                        graph.find_one(
                            label=floor,
                            property_key='office_name',
                            property_value=row[11]
                        )
                    )
                )
            if row[12].find(u'，') != -1:
                card_arr = row[12].split(u'，')
                for sub_card in card_arr:
                    if sub_card == "":
                        continue
                    graph.create(
                        Relationship(
                            graph.find_one(
                                label=room,
                                property_key='office_name',
                                property_value=row[0]
                            ),
                            '证件',
                            graph.find_one(
                                label=card,
                                property_key='office_name',
                                property_value=sub_card
                            )
                        )
                    )
            else:
                if row[12] == 'nan':
                    continue
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=row[0]),
                        '证件',
                        graph.find_one(
                            label=card,
                            property_key='office_name',
                            property_value=row[12]
                        )
                    )
                )
            mark_list = row[18].split(u',')
            dis_list = str(row[13]).split('_')
            dir_list = str(row[17]).split(u';')
            for j in range(len(mark_list)):
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=row[0]),
                        '相邻',
                        graph.find_one(
                            label=mark,
                            property_key='office_name',
                            property_value=mark_list[j]
                        ),
                        dis=dis_list[j],
                        dir=dir_list[j]
                    )
                )
        except AttributeError as e:
            print('room', e)

    for i in range(len(room_values) - 6, len(room_values)):
        try:
            row = room_values[i].split(' ')
            mark_list = row[18].split(u',')
            dis_list = str(row[13]).split(u'_')
            dir_list = str(row[17]).split(u';')
            x_list = row[15].split(u';')
            y_list = row[16].split(u';')
            for j in range(len(mark_list)):
                if len(x_list) > 0 and len(y_list) > 0:
                    graph.create(
                        Relationship(
                            graph.find_one(
                                label=mark,
                                property_key='office_name',
                                property_value=row[0]
                            ),
                            '互连',
                            graph.find_one(
                                label=mark,
                                property_key='office_name',
                                property_value=mark_list[j]
                            ),
                            dis=dis_list[j], dir=dir_list[j],
                            x=x_list[j], y=y_list[j]
                        )
                    )

            graph.create(
                Relationship(
                    graph.find_one(
                        label=mark,
                        property_key='office_name',
                        property_value=row[0]
                    ),
                    '处于',
                    graph.find_one(
                        label=area,
                        property_key='office_name',
                        property_value=row[10])
                )
            )

            graph.create(
                Relationship(graph.find_one(
                    label=mark,
                    property_key='office_name',
                    property_value=row[0]
                ),
                    '位于',
                    graph.find_one(
                        label=floor,
                        property_key='office_name',
                        property_value=row[11]
                    )
                )
            )

        except AttributeError as e:
            print('room', e)

    # 建立楼层联系
    floor_values = floor_data.split('@')
    row = ['']
    for i in range(1, len(floor_values)):
        try:
            row = floor_values[i].split(' ')
            graph.create(
                Relationship(
                    graph.find_one(
                        label=floor,
                        property_key='office_name',
                        property_value=row[0]),
                    '处于',
                    graph.find_one(
                        label=area,
                        property_key='office_name',
                        property_value=row[2]
                    )
                )
            )
        except AttributeError as e:
            print('2', row[0], e, row[2])

    # 建立资源联系
    res_values = resource_data.split('@')
    for i in range(1, len(res_values)):
        try:
            room_arr = []
            row = res_values[i].split(' ')
            graph.create(
                Relationship(
                    graph.find_one(
                        label=resource,
                        property_key='office_name',
                        property_value=row[0]
                    ),
                    '属于',
                    graph.find_one(
                        label=restype,
                        property_key='office_name',
                        property_value=row[5]
                    )
                )
            )
            if row[4].find(u'，') != -1:
                room_arr = row[4].split(u'，')
            else:
                room_arr.append(row[4])

            for sub_room in room_arr:
                graph.create(
                    Relationship(graph.find_one(
                        label=resource,
                        property_key='office_name',
                        property_value=row[0]
                    ), '存放',
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=sub_room
                        )
                    )
                )
        except AttributeError as e:
            print('3', row[0], e, row[4], row[5])

    # 建立精品联系
    goods_values = goods_data.split('@')
    for i in range(1, len(goods_values)):
        try:
            room_arr = []
            row = goods_values[i].split(' ')
            graph.create(
                Relationship(
                    graph.find_one(
                        label=goods,
                        property_key='office_name',
                        property_value=row[0]
                    ),
                    '属于',
                    graph.find_one(
                        label=resource,
                        property_key='office_name',
                        property_value=row[5]
                    )
                )
            )
            if row[4].find(u'，') != -1:
                room_arr = row[4].split(u'，')
            else:
                room_arr.append(row[4])

            for sub_room in room_arr:
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=goods,
                            property_key='office_name',
                            property_value=row[0]
                        ),
                        '存放',
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=sub_room
                        )
                    )
                )

        except AttributeError as e:
            print('goods', row[0], e, row[4], row[5])

    # 建立资源类型联系
    res_type_values = resType_data.split('@')
    for i in range(1, len(res_type_values)):
        try:
            row = res_type_values[i].split(' ')
            graph.create(
                Relationship(
                    graph.find_one(
                        label=restype,
                        property_key='office_name',
                        property_value=row[0]
                    ),
                    '属于',
                    graph.find_one(
                        label=res,
                        property_key='office_name',
                        property_value=row[5]
                    )
                )
            )
            room_arr = []
            if row[6] == 'nan':
                continue
            if row[6].find(u'，') != -1:
                room_arr = row[6].split(u'，')
            else:
                room_arr.append(row[6])
            for sub_room in room_arr:
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=restype,
                            property_key='office_name',
                            property_value=row[0]
                        ), '存放',
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=sub_room
                        )
                    )
                )

            graph.create(
                Relationship(
                    graph.find_one(
                        label=restype,
                        property_key='office_name',
                        property_value=row[0]
                    ),
                    '属于',
                    graph.find_one(
                        label=mul_type,
                        property_key='office_name',
                        property_value=row[7]
                    )
                )
            )
        except AttributeError as e:
            print('-----------', row[0], e, row[7])

    # 建立服务联系
    service_values = service_data.split('@')

    for i in range(1, len(service_values)):
        try:
            row = service_values[i].split(' ')
            room_arr = []
            if row[2].find(u'，') != -1:
                room_arr = row[2].split(u'，')
            else:
                room_arr.append(row[2])
            for sub_room in room_arr:
                if sub_room == 'nan':
                    continue
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=service,
                            property_key='office_name',
                            property_value=row[0]
                        ), '发生',
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=sub_room
                        )
                    )
                )
        except AttributeError as e:
            print('-----------', row[0], e)
    # 建立业务联系
    task_values = task_data.split('@')

    for i in range(1, len(task_values)):
        try:
            row = task_values[i].split(' ')
            room_arr = []
            if row[2].find(u'，') != -1:
                room_arr = row[2].split(u'，')
            else:
                room_arr.append(row[2])

            for sub_room in room_arr:
                if sub_room == 'nan':
                    continue
                graph.create(
                    Relationship(
                        graph.find_one(
                            label=task,
                            property_key='office_name',
                            property_value=row[0]
                        ), '办理',
                        graph.find_one(
                            label=room,
                            property_key='office_name',
                            property_value=sub_room
                        )
                    )
                )
        except AttributeError as e:
            print('-----------', row[0], e)
    # 建立国家图书馆联系
    graph.create(
        Relationship(
            graph.find_one(
                label=library, property_key='office_name',
                property_value=library
            ), '包括',
            graph.find_one(
                label=area, property_key='office_name',
                property_value='总馆北区'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(
                label=library, property_key='office_name',
                property_value=library
            ), '包括',
            graph.find_one(
                label=area, property_key='office_name',
                property_value='总馆南区'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(
                label=library, property_key='office_name',
                property_value=library
            ), '包括',
            graph.find_one(
                label=area, property_key='office_name',
                property_value='文津楼'
            )
        )
    )

    graph.create(
        Relationship(
            graph.find_one(
                label=library, property_key='office_name',
                property_value=library
            ), '包括',
            graph.find_one(
                label=area, property_key='office_name',
                property_value='临琼楼'
            )
        )
    )


def clear_db():
    """
    清空数据数据库数据
    :return:
    """
    print('deleting all data:')
    graph.delete_all()
    print('finish deleting data')


def init_db():
    """
    往数据库中导入数据, 会先清空数据库
    :return:
    """
    # 建图
    clear_db()
    print('importing data:')
    create_node()
    create_relation()
    create()
    print('finish importing data')
